package com.pinganfu.tesla.iwallet.tab;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.pinganfu.tesla.iwallet.R;

public class TabHomeActivity extends Activity {

	private GridView mGridView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tab_home);

		mGridView = (GridView) findViewById(R.id.gridView1);

		// initGridView();
		mGridView.setAdapter(new GridViewAdapter(this));
	}

	private class GridViewAdapter extends BaseAdapter {

		private Context context;
		private ArrayList<HashMap<String, Object>> mListImageItem = new ArrayList<HashMap<String, Object>>();
		private LayoutInflater mInflater = null;

		public GridViewAdapter(Context context) {
			this.context = context;

			mInflater = LayoutInflater.from(context);

			String[] item_names = new String[]{"活钱宝", "转账", "信用卡还款", "手机充值", "水电煤", "彩票", "收款", "更多"};
			int[] item_resources = new int[]{R.drawable.ic_grid_hqb, R.drawable.ic_grid_transfer, R.drawable.ic_grid_creditcard, R.drawable.ic_grid_mobile, R.drawable.ic_grid_fee,
					R.drawable.ic_grid_lottory, R.drawable.ic_grid_receive, R.drawable.ic_grid_more};
			for (int i = 0; i < item_names.length; i++) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("item_image", item_resources[i]);
				map.put("item_text", item_names[i]);
				mListImageItem.add(map);
			}
		}

		@Override
		public int getCount() {
			return mListImageItem.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = mInflater.inflate(R.layout.grid_item, null);
			view.setBackgroundColor(Color.WHITE);
			HashMap<String, Object> item = mListImageItem.get(position);

			int resId = Integer.parseInt(item.get("item_image").toString());
			ImageView image = (ImageView) view.findViewById(R.id.item_image);
			image.setImageDrawable(this.context.getResources().getDrawable(resId));

			TextView tv = (TextView) view.findViewById(R.id.item_text);
			tv.setText(item.get("item_text").toString());

			// TextView result = new TextView(context);
			// result.setText("Item " + position);
			// result.setTextColor(Color.BLACK);
			// result.setTextSize(24);
			// result.setLayoutParams(new AbsListView.LayoutParams(new
			// LayoutParams(LayoutParams.FILL_PARENT,
			// LayoutParams.WRAP_CONTENT)));
			// result.setGravity(Gravity.CENTER);
			// result.setBackgroundColor(Color.WHITE); // 设置背景颜色
			return view;
		}
	}

	// private void initGridView() {
	//
	// // 生成动态数组，并且转入数据
	// ArrayList<HashMap<String, Object>> listImageItem = new
	// ArrayList<HashMap<String, Object>>();
	//
	// String[] item_names = new String[]{"活钱宝", "转账", "信用卡还款", "手机充值", "水电煤",
	// "彩票", "更多"};
	// int[] item_resources = new int[]{R.drawable.ic_grid_hqb,
	// R.drawable.ic_grid_transfer, R.drawable.ic_grid_creditcard,
	// R.drawable.ic_grid_mobile, R.drawable.ic_grid_fee,
	// R.drawable.ic_grid_lottory, R.drawable.ic_grid_more};
	// for (int i = 0; i < item_names.length; i++) {
	// HashMap<String, Object> map = new HashMap<String, Object>();
	// map.put("item_image", item_resources[i]);
	// map.put("item_text", item_names[i]);
	// listImageItem.add(map);
	// }
	//
	// SimpleAdapter saImageItems = new SimpleAdapter(this, listImageItem,
	// R.layout.grid_item, new String[]{"item_image", "item_text"}, new
	// int[]{R.id.item_image, R.id.item_text});
	// mGridView.setAdapter(saImageItems);
	// // 添加消息处理
	// mGridView.setOnItemClickListener(new ItemClickListener());
	// }
	//
	// // 当AdapterView被单击(触摸屏或者键盘)，则返回的Item单击事件
	// class ItemClickListener implements OnItemClickListener {
	// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long
	// arg3) {
	//
	// }
	// }
}
